package reading.web.controllers;

import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reading.common.pojos.EntityResponse;
import reading.data.DataUtils;
import reading.data.model.Book;
import reading.mybatis.Page;
import reading.mybatis.Pageable;
import reading.service.BookService;

import static reading.common.pojos.EntityResponse.success;

/**
 * ${DESCRIPTION}
 *
 * @author txw
 * @since 2019-02-15 12:43
 **/
@RestController
@RequestMapping("/inapi/book")
public class BookController {
    private final BookService bookService;
    @Autowired
    public BookController(BookService bookService) {
        this.bookService = bookService;
    }

    @GetMapping("findPage")
    @ApiOperation("分页查询书籍 信息 ")
    public EntityResponse<Page<Book>> findPage(@RequestParam(value = "page",
            required = false, defaultValue = Pageable.STR_PAGE_INDEX) Integer page,
                                               @RequestParam(value = "size", required = false, defaultValue = Pageable.STR_PAGE_SIZE) Integer size,
                                               @RequestParam(value = "orderField", required = false,defaultValue = "create_time") String orderField,
                                               @RequestParam(value = "direction", required = false, defaultValue = "desc") String direction){
        Pageable pageable = DataUtils.pageable(page, size, orderField, direction);
        return success(bookService.findPage(pageable));
    }
}
